/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.ExecHardwareInterface;
import br.gov.cemaden.library.model.smp.ExecHardware;
import br.gov.cemaden.library.model.smp.WhoSys;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class ExecHardwareDao extends GenericDao implements ExecHardwareInterface {

    private Session session;

    public ExecHardwareDao(Session session) {
        this.session = session;
    }

    public List<ExecHardware> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ExecHardware> execHardware = null;
        try {
            Query query = session.getNamedQuery("ExecHardware.findAll");
            execHardware = (List<ExecHardware>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecHardwareDao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execHardware;
    }

    public ExecHardware findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ExecHardware execHardware = null;
        try {
            Query query = session.getNamedQuery("ExecHardware.findById").setParameter("id", id);
            execHardware = (ExecHardware) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecHardwareDao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execHardware;
    }

    public List<ExecHardware> findByData(Date data) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ExecHardware> execHardware = null;
        try {
            Query query = session.getNamedQuery("ExecHardware.findByData").setParameter("data", data);
            execHardware = (List<ExecHardware>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecHardwareDao.findByData: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execHardware;
    }

    public List<ExecHardware> findByWhoSys(WhoSys who) {
        Transaction tx = session.beginTransaction();
        List<ExecHardware> execHardware = null;
        try {
            Query query = session.getNamedQuery("ExecHardware.findByUsuarioSistema").setParameter("WhoSys", who);
            execHardware = (List<ExecHardware>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecHardwareDao.findByWhoSys: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execHardware;

    }

    public ExecHardware insert(ExecHardware execHardware) {
        try {
            saveOrUpdate(this.session, execHardware);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return execHardware;
    }

    public ExecHardware update(ExecHardware execHardware) {
        try {
            saveOrUpdate(this.session, execHardware);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return execHardware;
    }

    public void delete(ExecHardware execHardware) {
        try {
            remove(this.session, execHardware);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
}
